using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;



namespace Bloks
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private SoundEffect soundEngine;
        private Player1 player1;
        private ScrollingBackground myBackground;
        private double startsidst = 0;
        private Levels level;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            soundEngine = Content.Load<SoundEffect>("FFF - Nero 7.xxkg");
            soundEngine.Play();

            player1 = new Player1();
            Texture2D player_texture = Content.Load<Texture2D>("player1");
            player1.LoadContent(GraphicsDevice, player_texture);

            level = new Levels();
            Texture2D test2 = Content.Load<Texture2D>("level1");
            level.GetLevel(test2);

            Texture2D texture = Content.Load<Texture2D>("deathbox");
            level.Load(texture);

            myBackground = new ScrollingBackground();
            Texture2D background = Content.Load<Texture2D>("background");
            myBackground.Load(GraphicsDevice, background);
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            //Keyboard states
            KeyboardState keyboard = Keyboard.GetState();
            //exit
            if (keyboard.IsKeyDown(Keys.Escape))
                this.Exit();

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            float detlaY = elapsed * 200;

            player1.Update(gameTime.TotalGameTime.TotalSeconds, level);
            myBackground.Update(detlaY);
            level.Update(detlaY);

            if (soundEngine.Duration.TotalSeconds < gameTime.TotalGameTime.TotalSeconds - startsidst)
            {
                soundEngine.Play();
                startsidst = gameTime.TotalGameTime.TotalSeconds;
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            myBackground.Draw(spriteBatch);
            player1.Draw(spriteBatch);
            level.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
